//
//  SchollFriendManagerViewController.m
//  ECBBusinessCamp
//
//  Created by rimi on 16/6/27.
//  Copyright © 2016年 邓毕华. All rights reserved.
//

#import "SchollFriendManagerViewController.h"

#import "SchollFriendCollectionViewCell.h"
#import "SchollFriendManagerDetailViewController.h"

static NSString * const kSchollFriendCollectionViewCellIdentifier = @"schollFriendCollectionViewCellIdentifier";

@interface SchollFriendManagerViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, strong) UICollectionView * schollFriendCollectionView; // 校友集合视图
@property (nonatomic, strong) UICollectionViewFlowLayout * layout; // 流式布局
@property (nonatomic, strong) NSArray * dataSource; // 数据源

@end

@implementation SchollFriendManagerViewController

#pragma mark 🌷🌷🌷life cycle🌷🌷🌷
- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 添加视图
    [self.view addSubview:self.schollFriendCollectionView];
    
    // 自动布局
    [_schollFriendCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(SCREEN_HEIGHT * 0.092);
        make.bottom.offset(SCREEN_HEIGHT * -0.09);
        make.left.offset(SCREEN_WIDTH * 0.195);
        make.right.offset(0);
    }];
    
    // 加载数据
    [self loadData];
}

#pragma mark 🌷🌷🌷UICollectionViewDataSource🌷🌷🌷
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _dataSource.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    SchollFriendCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:kSchollFriendCollectionViewCellIdentifier forIndexPath:indexPath];
    
    // 单元格赋值
    [cell.headImageView sd_setImageWithURL:[NSURL URLWithString:BASEURLAPPEND(_dataSource[indexPath.row][@"friendHead"])] placeholderImage:[UIImage imageNamed:@"default_head_s.png"]];
    cell.nameLabel.text = _dataSource[indexPath.row][@"friendName"];
    
    return cell;
}

#pragma mark 🌷🌷🌷UICollectionViewDelegate🌷🌷🌷
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    // 添加聊天视图与控制器
    SchollFriendManagerDetailViewController * schoolFriendManagerDetailVc = [[SchollFriendManagerDetailViewController alloc] init];
    schoolFriendManagerDetailVc.nameLabel.text = _dataSource[indexPath.row][@"friendName"];
    schoolFriendManagerDetailVc.friendid = _dataSource[indexPath.row][@"friendid"];
    
    [self addChildViewController:schoolFriendManagerDetailVc];
    [self.view addSubview:schoolFriendManagerDetailVc.view];
}

#pragma mark 🌷🌷🌷private methods🌷🌷🌷
- (void)loadData {
    NSMutableDictionary * paramters = [NSMutableDictionary dictionary];
    
    [[NetTool sharedTool] requestMethod:POST loadingMsg:@"加载中..." url:BASEURLAPPEND(PERSONALHOMEPAGE_SYSTEMMESSAGE_FRIENDLIST_URL) parameters:paramters finish:^(id responseObject, NSError *error) {
        if (!error) {
            if ([responseObject[@"status"] integerValue] == 1) {
                [MBProgressHUD showSuccess:@"加载完成！"];
                _dataSource = responseObject[@"data"];
                [_schollFriendCollectionView reloadData];
            } else {
                [MBProgressHUD showSuccess:@"加载失败！"];
            }
        } else {
            [MBProgressHUD showError:[[NSString stringWithFormat:@"%@", error] containsString:@"The request timed out."] ? @"登录超时" : @"I don't know"];
        }
    }];
}

#pragma mark 🌷🌷🌷getters and setters🌷🌷🌷
- (UICollectionViewFlowLayout *)layout {
    if (!_layout) {
        _layout = [[UICollectionViewFlowLayout alloc] init];
        _layout.itemSize = CGSizeMake(SCREEN_WIDTH * 0.09, SCREEN_WIDTH * 0.12);
        _layout.minimumInteritemSpacing = 40;
        _layout.minimumLineSpacing = 50;
        _layout.scrollDirection = UICollectionViewScrollDirectionVertical;
        _layout.sectionInset = UIEdgeInsetsMake(60, 100, 10, 100);
    }
    return _layout;
}
- (UICollectionView *)schollFriendCollectionView {
    if (!_schollFriendCollectionView) {
        _schollFriendCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) collectionViewLayout:self.layout];
        _schollFriendCollectionView.backgroundColor = [UIColor whiteColor];
        
        // 设置数据源
        _schollFriendCollectionView.dataSource = self;
        // 设置代理
        _schollFriendCollectionView.delegate = self;
        
        // 注册单元格
        [_schollFriendCollectionView registerClass:[SchollFriendCollectionViewCell class] forCellWithReuseIdentifier:kSchollFriendCollectionViewCellIdentifier];
    }
    return _schollFriendCollectionView;
}

@end
